//
//  YXMachinesDropMenuView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/29.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMachinesDropMenuView.h"

@interface YXMachinesDropMenuView ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *backView;

@property (weak, nonatomic) IBOutlet UILabel *toLab;
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
@property (nonatomic ,strong) UIButton *activationBtn;
@property (nonatomic ,strong) UIButton *inactiveBtn;

// 记录激活按钮的状态
@property (nonatomic ,strong) NSString *state;
@end
@implementation YXMachinesDropMenuView

- (UIButton *)activationBtn {
    if (!_activationBtn) {
        _activationBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _activationBtn.frame = CGRectMake(30, 30, 100, 20);
        [_activationBtn setTitle:@"已激活" forState:(UIControlStateNormal)];
        [_activationBtn setTitleColor:color_textOne forState:(UIControlStateNormal)];
        [_activationBtn setImage:[UIImage imageNamed:@"未激活"] forState:(UIControlStateNormal)];
        [_activationBtn setImage:[UIImage imageNamed:@"已激活"] forState:(UIControlStateSelected)];
        _activationBtn.selected = YES;
        [_activationBtn addTarget:self action:@selector(activationBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _activationBtn;
}

- (UIButton *)inactiveBtn {
    if (!_inactiveBtn) {
        _inactiveBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _inactiveBtn.frame = CGRectMake(kScreenWidth/2 + 30, 30, 100, 20);
        [_inactiveBtn setTitle:@"未激活" forState:(UIControlStateNormal)];
        [_inactiveBtn setTitleColor:color_textOne forState:(UIControlStateNormal)];
        [_inactiveBtn setImage:[UIImage imageNamed:@"未激活"] forState:(UIControlStateNormal)];
        [_inactiveBtn setImage:[UIImage imageNamed:@"已激活"] forState:(UIControlStateSelected)];
        [_inactiveBtn addTarget:self action:@selector(activationBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _inactiveBtn;
}

- (void)activationBtnAction:(UIButton *)sender {
    sender.selected = ! sender.selected;
    if (sender == _activationBtn) {
        _inactiveBtn.selected = NO;
        _activationBtn.selected = YES;
        _state = @"1";
    }else {
        _activationBtn.selected = NO;
        _inactiveBtn.selected = YES;
        _state = @"0";

    }
    
}

- (UITextField *)typeTF {
    if (!_typeTF) {
        _typeTF = [[UITextField alloc] initWithFrame:(CGRectMake(20, 30, kScreenWidth - 40, 30))];
        _typeTF.borderStyle = UITextBorderStyleLine;
        _typeTF.placeholder = @"请输入机具类型";
        _typeTF.font = [UIFont systemFontOfSize:16];
        _typeTF.textAlignment = NSTextAlignmentCenter;
        _typeTF.delegate = self;
        [self addSubview: _typeTF];
    }
    return _typeTF;
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXMachinesDropMenuView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
        self.backgroundColor = [UIColor py_colorWithHexString:@"#000000" alpha:0.4];
        self.searchBtn.layer.masksToBounds = YES;
        self.searchBtn.layer.cornerRadius = 8.0f;
        self.starTF.delegate = self;
        self.endTF.delegate = self;
        [self addSubview: self.typeTF];
        [self addSubview:self.inactiveBtn];
        [self addSubview:self.activationBtn];
        
        // 默认已激活
        self.state = @"1";
    }
    return self;
    
}

- (void)setType:(NSInteger)type {
    _type = type;
    if (self.type == 0) {
        self.starTF.hidden = NO;
        self.endTF.hidden = NO;
        self.toLab.hidden = NO;
        self.typeTF.hidden = YES;
        self.inactiveBtn.hidden = YES;
        self.activationBtn.hidden = YES;
    }else if (self.type == 1) {
        self.starTF.hidden = YES;
        self.endTF.hidden = YES;
        self.toLab.hidden = YES;
        self.inactiveBtn.hidden = YES;
        self.activationBtn.hidden = YES;
        self.typeTF.hidden = NO;
    }else {
        self.starTF.hidden = YES;
        self.endTF.hidden = YES;
        self.toLab.hidden = YES;
        self.typeTF.hidden = YES;
        self.inactiveBtn.hidden = NO;
        self.activationBtn.hidden = NO;
        
    }
}

#pragma mark - 点击查询
- (IBAction)searchBtnAction:(UIButton *)sender {
    if (_type == 0) {
        self.clickCodeSearchBtnBlock(self.starTF.text, self.endTF.text);
    }else if (_type == 1) {
        self.clickModelSearchBtnBlock(self.typeTF.text);
    }else {
        self.clickStateSearchBtnBlock(self.state);
    }
    [self.typeTF resignFirstResponder];
    [self.starTF resignFirstResponder];
    [self.endTF resignFirstResponder];
    self.alpha = 0;
}

#pragma mark -TextField代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    return [textField resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.typeTF resignFirstResponder];
    [self.starTF resignFirstResponder];
    [self.endTF resignFirstResponder];
    self.alpha = 0;
    
}

@end
